package com.ruoyi.quartz.task;

import com.crm.custinfo.service.ICustNoticeInfoService;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.framework.datasource.DynamicRoutingDataSource;
import com.ruoyi.tenant.domain.MasterTenant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 每天凌晨2点初始化零星跟进定时通知消息
 * 针对部长、实习部长、业务员、实习业务员
 */
@Slf4j
@Component("noticeTask")
public class NoticeInTimeTask {
    @Autowired
    private ICustNoticeInfoService iCustNoticeInfoService;
    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    /**
     * 每天凌晨2点初始化零星跟进定时通知消息
     * 针对部长、实习部长、业务员、实习业务员
     *
     */
    public void  initNoticeIntime(){
        DynamicDataSourceContextHolder.setDataSourceKey("zk");
        MasterTenant masterTenant = new MasterTenant();
        masterTenant.setStatus("1");
        //获取所有启用的租户,并补全数据库链接信息
        List<MasterTenant> masterTenants = dynamicRoutingDataSource.getTenantListLoadDataSource(masterTenant);
        for (MasterTenant masterTenant1 : masterTenants) {
            if("zk".equals(masterTenant1.getTenant())){
                //跳过总库租户
                continue;
            }
            DynamicDataSourceContextHolder.setDataSourceKey(masterTenant1.getTenant());
            log.info("初始化零星跟进定时通知消息，当前线程数据库："+DynamicDataSourceContextHolder.getDataSourceKey());
            iCustNoticeInfoService.initNoticeIntime();
        }
    }
    /**
     * 功能说明: 定时消息推送
     * 系统版本: v1.0
     * 开发人员: Mr.Wen
     * 开发时间: 2024/02/19
     */
    public void  pushCustNotice(){


        DynamicDataSourceContextHolder.setDataSourceKey("zk");
        MasterTenant masterTenant = new MasterTenant();
        masterTenant.setStatus("1");
        //获取所有启用的租户,并补全数据库链接信息
        List<MasterTenant> masterTenants = dynamicRoutingDataSource.getTenantListLoadDataSource(masterTenant);
        for (MasterTenant masterTenant1 : masterTenants) {
            if("zk".equals(masterTenant1.getTenant())){
                //跳过总库租户
                continue;
            }
            DynamicDataSourceContextHolder.setDataSourceKey(masterTenant1.getTenant());
            log.info("开始推送通知，当前线程数据库："+DynamicDataSourceContextHolder.getDataSourceKey());
            iCustNoticeInfoService.pushCustNotice();
        }
    }


}
